package com.zx1316.javasimpledoudizhu.server.ui;

import com.zx1316.javasimpledoudizhu.server.Server;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class ServerController {
    public Label name1;
    public Label name2;
    public Label name3;
    public TextField addr;
    public TextField port;
    public TextField password;
    public Button start;
    public Button stop;
    public CheckBox allowDouble;
    public CheckBox tips;
    public TextField rounds;
    public VBox mainConfig;
    public VBox playerConfig;
    public HBox gameConfig;
    public CheckBox allowChat;
    private Server server;

    public void onStartButtonClick() {
        int port1, round;
        try {
            port1 = Integer.parseInt(port.getText());
        } catch (NumberFormatException e) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("端口号格式错误！");
            alert.showAndWait();
            return;
        }
        try {
            round = Integer.parseInt(rounds.getText());
        } catch (NumberFormatException e) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("局数格式错误！");
            alert.showAndWait();
            return;
        }
        if (round < 1) {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setHeaderText("局数不能小于1！");
            alert.showAndWait();
            return;
        }
        server = new Server(round, tips.isSelected(), allowDouble.isSelected(), allowChat.isSelected(), this);
        server.startServer(addr.getText(), port1, password.getText());
        if (server.isStart()) {
            mainConfig.setDisable(true);
            gameConfig.setDisable(true);
            start.setDisable(true);
            stop.setDisable(false);
        }
    }

    public void onStopButtonClick() {
        if (server != null) {
            server.stopServer(true);
            if (!server.isStart()) {
                name1.setText("");
                name2.setText("");
                name3.setText("");
                mainConfig.setDisable(false);
                playerConfig.setDisable(false);
                gameConfig.setDisable(false);
                start.setDisable(false);
                stop.setDisable(true);
            }
        }
    }

    public void onKick3ButtonClick() {
        if (Server.getClients()[2] != null) {
            Server.getClients()[2].getWriter().println("KICK");
            Server.getClients()[2].getWriter().flush();
        }
    }

    public void onKick2ButtonClick() {
        if (Server.getClients()[1] != null) {
            Server.getClients()[1].getWriter().println("KICK");
            Server.getClients()[1].getWriter().flush();
        }
    }

    public void onKick1ButtonClick() {
        if (Server.getClients()[0] != null) {
            Server.getClients()[0].getWriter().println("KICK");
            Server.getClients()[0].getWriter().flush();
        }
    }
}
